<?php

require_once(Mage::getBaseDir('lib')."/Excel/PHPExcel.php");
require_once(Mage::getBaseDir('lib')."/Excel/PHPExcel/Writer/Excel2007.php");

class Silk_Zgb_Adminhtml_CreditmemoController extends Mage_Adminhtml_Controller_Action
{
	protected function _initAction()
	{
		$this->loadLayout()->_setActiveMenu("sales/creditmemo_list")->_addBreadcrumb(Mage::helper("adminhtml")->__("退款商品清单"),Mage::helper("adminhtml")->__("退款商品清单"));
		return $this;
	}
	public function indexAction()
	{
        $this->_title($this->__("轻松老板"));
	    $this->_title($this->__("退款商品清单"));

		$this->_initAction();
		$this->renderLayout();
	}

	public function gridAction()
	{
		$this->loadLayout()->renderLayout();
	}

	public function newGridAction()
	{
		$this->loadLayout()->renderLayout();
	}

    public function getWriter()
    {

        //add by wanglin  start 默认导出1个月的数据
        $startdate = date('Y-m-d',strtotime('-10day'));
        $enddate = date('Y-m-d',strtotime('1day'));
        $items = Mage::getResourceModel('sales/order_creditmemo_item_collection');
        $items->addFieldToSelect(array('*'));
        $select = $items->getSelect();
        $select->joinLeft(array('grid'=>'sales_flat_creditmemo_grid'),"main_table.parent_id=grid.entity_id",array('increment_id'=>'grid.increment_id',
            'order_id'=>'grid.order_increment_id',
            'created_at'=>'grid.created_at'));
        $select->joinLeft(array('order' => 'sales_flat_order'), "order.entity_id=grid.order_id", array('store_name'=>'order.customer_lastname'));
        $items->addFieldToFilter('grid.created_at',array('from'=>"$startdate",'to'=>"$enddate"));
        //add by wanglin  end 默认导出1个月的数据
        $objPHPExcel  = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->setTitle("退款商品清单");
        $sheet->getColumnDimension('A')->setWidth(15);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(60);
        $sheet->getColumnDimension('D')->setWidth(15);
        $sheet->getColumnDimension('E')->setWidth(10);
        $sheet->getColumnDimension('F')->setWidth(10);
        $sheet->getColumnDimension('G')->setWidth(10);
        $sheet->getColumnDimension('H')->setWidth(30);

        $sheet->getStyle('A1')->getFont()->setSize(13);
        $sheet->getStyle('A1')->getFont()->setBold(true);

        $sheet->getStyle('B1')->getFont()->setSize(13);
        $sheet->getStyle('B1')->getFont()->setBold(true);

        $sheet->getStyle('C1')->getFont()->setSize(13);
        $sheet->getStyle('C1')->getFont()->setBold(true);

        $sheet->getStyle('D1')->getFont()->setSize(13);
        $sheet->getStyle('D1')->getFont()->setBold(true);

        $sheet->getStyle('E1')->getFont()->setSize(13);
        $sheet->getStyle('E1')->getFont()->setBold(true);

        $sheet->getStyle('F1')->getFont()->setSize(13);
        $sheet->getStyle('F1')->getFont()->setBold(true);

        $sheet->getStyle('G1')->getFont()->setSize(13);
        $sheet->getStyle('G1')->getFont()->setBold(true);

        $sheet->getStyle('H1')->getFont()->setSize(13);
        $sheet->getStyle('H1')->getFont()->setBold(true);


        $sheet->getRowDimension('1')->setRowHeight(20);

        $styleThinBlackBorderOutline = array(
            'borders' => array (
               'outline' => array (
                  'style' => PHPExcel_Style_Border::BORDER_THIN,
                  'color' => array ('argb' => 'FF000000'),
              ),
           ),
        );

        $row = 1;
        $sheet->setCellValue("A{$row}","订单编号");
        $sheet->setCellValue("B{$row}","退款时间");
        $sheet->setCellValue("C{$row}","名称");
        $sheet->setCellValue("D{$row}","SKU");
        $sheet->setCellValue("E{$row}","数量");
        $sheet->setCellValue("F{$row}","价格");
        $sheet->setCellValue("G{$row}","小计");
        $sheet->setCellValue("H{$row}","店铺名");

        $sheet->getStyle("A{$row}")->applyFromArray($styleThinBlackBorderOutline);
        $sheet->getStyle("B{$row}")->applyFromArray($styleThinBlackBorderOutline);
        $sheet->getStyle("C{$row}")->applyFromArray($styleThinBlackBorderOutline);
        $sheet->getStyle("D{$row}")->applyFromArray($styleThinBlackBorderOutline);
        $sheet->getStyle("E{$row}")->applyFromArray($styleThinBlackBorderOutline);
        $sheet->getStyle("F{$row}")->applyFromArray($styleThinBlackBorderOutline);
        $sheet->getStyle("G{$row}")->applyFromArray($styleThinBlackBorderOutline);
        $sheet->getStyle("H{$row}")->applyFromArray($styleThinBlackBorderOutline);

        $row++;
        $checkout = Mage::helper('checkout');
        foreach($items as $item)
        {

            $sheet->setCellValue("A{$row}",$item->getOrderId());
            $sheet->setCellValue("B{$row}",$item->getCreatedAt());
            $sheet->setCellValue("C{$row}",$item->getName());
            $sheet->setCellValue("D{$row}",$item->getSku());
            $sheet->setCellValue("E{$row}",$item->getQty());
            $sheet->setCellValue("F{$row}",$item->getPrice());
            $sheet->setCellValue("G{$row}",$item->getRowTotal());
            $sheet->setCellValue("H{$row}",$item->getStoreName());



            $sheet->getStyle("A{$row}")->applyFromArray($styleThinBlackBorderOutline);
            $sheet->getStyle("B{$row}")->applyFromArray($styleThinBlackBorderOutline);
            $sheet->getStyle("C{$row}")->applyFromArray($styleThinBlackBorderOutline);
            $sheet->getStyle("D{$row}")->applyFromArray($styleThinBlackBorderOutline);
            $sheet->getStyle("E{$row}")->applyFromArray($styleThinBlackBorderOutline);
            $sheet->getStyle("F{$row}")->applyFromArray($styleThinBlackBorderOutline);
            $sheet->getStyle("G{$row}")->applyFromArray($styleThinBlackBorderOutline);
            $sheet->getStyle("H{$row}")->applyFromArray($styleThinBlackBorderOutline);

            $row++;
        }

        return $objWriter;
    }

    public function exportAction()
    {
        $objWriter = $this->getWriter();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        //header("Content-Type:application/force-download");
        //header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream;charset=utf-8");
        //header("Content-Type:application/download");
        if(strpos($_SERVER['HTTP_USER_AGENT'],"MSIE"))
        {
            header('Content-Disposition:attachment;filename="'.urlencode("退款商品清单.xlsx").'"');
        }
        else
        {
            header('Content-Disposition:attachment;filename="退款商品清单.xlsx"');
        }

        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
    }

	public function newAction()
	{
		$this->loadLayout()->renderLayout();
	}

	protected function _isAllowed()
    {
    	return true;
        return Mage::getSingleton('admin/session')->isAllowed('newsletter/queue');
    }

    public function massStatusAction()
    {

    }
}
